package coreJob;

import static org.quartz.JobBuilder.newJob;

import java.util.List;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import listener.DefaultJobListener;
import listener.DefaultTriggerListener;

import org.apache.log4j.Logger;
import org.quartz.Job;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.Trigger;
import org.quartz.impl.StdSchedulerFactory;

import coreJob.common.ReflectInterface;
import coreJob.common.Tool;
/**
 * Job核心控制类
 * @author Ruoli
 * 2015/07/11
 */
public class Core implements ServletContextListener{
	Logger log = Logger.getLogger(Core.class);
	
	
	
	@SuppressWarnings("unchecked")
	public void contextInitialized(ServletContextEvent arg0) {
		 this.initJob();
	}
	
	@SuppressWarnings("unchecked")
	public  void initJob(){
		try {
			Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
			ReflectInterface reflectInterface = new ReflectInterface();
			List<Class<?>> ijobList = reflectInterface
					.getAllAssignedClass(IJob.class);
			DefaultTriggerListener triggerListener=new DefaultTriggerListener();
			DefaultJobListener jobListener=new DefaultJobListener();
			scheduler.getListenerManager().addTriggerListener(triggerListener);
			scheduler.getListenerManager().addJobListener(jobListener);
			for (Class<?> ijob : ijobList) {
				IJob ij = (IJob) ijob.newInstance();
				Class<? extends Job> job = (Class<? extends Job>) ijob;
				//查询此Job是否正在执行中，如果没有被执行，或 Job的执行完全被终止，则添加，否则跳过
				boolean isRun=Tool.ifRunJob(ij.setJobInfo().getJobName(),ij.setJobInfo().getJobGroup());
				if(isRun){
					continue;
				}
				JobDetail timeJob = newJob(job).withIdentity(
						ij.setJobInfo().getJobName(), ij.setJobInfo().getJobGroup()).build();
				Trigger jobTrigger=null;
				if(ij.setJobInfo().getTriggerType()==null){
					log.error("请验证TriggerType是否为有效值，"+ij.setJobInfo().getTriggerType()+"，发生Job为："+ijob.getName());
					continue;
				}else if(ij.setJobInfo().getTriggerType()==1){//每分钟一次
					jobTrigger = Tool.getMinutelyTrigger(ij.setJobInfo().getStartTime());
					
				}else if(ij.setJobInfo().getTriggerType()==2){//每小时一次
					jobTrigger = Tool.getHourlyTrigger(ij.setJobInfo().getStartTime());
					
				}else if(ij.setJobInfo().getTriggerType()==3){//每天一次
					jobTrigger = Tool.getDaylyTrigger(ij.setJobInfo().getStartTime());
					
				}else if(ij.setJobInfo().getTriggerType()==0 
						      && ij.setJobInfo().getCron()!=null){//corn表达式
					try{
						 jobTrigger = Tool.getCronTrigger(ij.setJobInfo().getStartTime(), ij.setJobInfo().getCron(), ij.setJobInfo().getCronDescription());
					}catch(Exception ex){
						log.error("请验证Cron是否为有效值，"+ij.setJobInfo().getCron()+"，发生Job为："+ijob.getName());
						
					}
				}else{
					log.error("请验证TriggerType是否为有效值，"+ij.setJobInfo().getTriggerType()+"，发生Job为："+ijob.getName());
					continue;
				}
				
				if(timeJob!=null && jobTrigger!=null){
					scheduler.scheduleJob(timeJob, jobTrigger);
				}
				
			}

			scheduler.start();

		} catch (Exception ex) {
			log.error("初始化Job信息发生错误，请查看错误详细信息");
			ex.printStackTrace();
		}
		
	}

	

	public void contextDestroyed(ServletContextEvent sce) {
		
	}
}